package cn.stylefeng.guns.modular.ry600.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
















import cn.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.stylefeng.guns.modular.system.model.BackOrder;
import cn.stylefeng.guns.modular.system.model.Client;
import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.system.model.OutOrderItem;
import cn.stylefeng.guns.modular.ry600.service.IBackOrderService;
import cn.stylefeng.guns.modular.ry600.service.IClientService;
import cn.stylefeng.guns.modular.ry600.service.IErpCustomerService;
import cn.stylefeng.guns.modular.ry600.service.IErpProductService;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;
import cn.stylefeng.guns.modular.ry600.service.IReviseOrderService;

/**
 * 连锁客户验证管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-06 18:35:42
 */
@Controller
@RequestMapping("/backorder")
public class BackOrderController extends BaseController {

//    private String PREFIX = "/ry600/client/";
	private String PREFIX = "/ry600/backorder/";

    @Autowired
    private IBackOrderService backOrderService;
    @Autowired
    private IErpkindService erpkindService;

    /**
     * 跳转到连锁客户验证管理首页
     */
    @RequestMapping("")
    public String index(HttpServletRequest request) {
//        return PREFIX + "client.html";
    	List<Erpkind> resultErpKind=erpkindService.selectList(null);
    	for(int i=0;i<resultErpKind.size();i++){
    		if("1".equals(resultErpKind.get(i).getIfMain())){
    			request.getSession().setAttribute("dataSource", resultErpKind.get(i).getAccountId());
    			break;
    		}
    	}
    	return PREFIX + "backorder.html";
    }

    
    
    /**
     * 连锁客户条件查找
     * @throws ParseException 
     */
    @RequestMapping(value = "/selectByMsg")
    @ResponseBody
    public Object selectByMsg(HttpServletRequest request, HttpServletResponse response) throws ParseException {
    	
    	if(!StringUtils.isBlank(request.getParameter("page")) && !StringUtils.isBlank(request.getParameter("rows"))){
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer rows = Integer.valueOf(request.getParameter("rows"));
			Map<String, Object> msg=this.setMap(request);
			PageHelper.startPage(page, rows);
			List<BackOrder> result=(List<BackOrder>) backOrderService.selectByMsg(msg,(String)msg.get("dataSource"));
			PageInfo<BackOrder> pageInfo = new PageInfo<BackOrder>(result);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rows", pageInfo.getList());
			map.put("total", pageInfo.getTotal());
			return map;
    		
    	}
    	Map<String, Object> msg=this.setMap(request);
    	Object result=backOrderService.selectByMsg(msg,(String)msg.get("dataSource"));
    	
    	return result;
        
    }
    
    public Map<String,Object> setMap(HttpServletRequest request) throws ParseException{
    	Map<String, Object> msg = new HashMap<String, Object>();
    	String ifBind=request.getParameter("ifBind");
    	String dataSource=request.getParameter("dataSource");
    	String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		String beginCreateTime = request.getParameter("beginCreateTime");
		String endCreateTime = request.getParameter("endCreateTime");
		if (!StringUtils.isEmpty(beginCreateTime)) {
			msg.put("beginCreateTime",
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new SimpleDateFormat("yyyy-MM-dd").parse(beginCreateTime)));
		}
		if (!StringUtils.isEmpty(endCreateTime)) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(endCreateTime));
			cal.add(Calendar.DATE, 1);
			cal.add(Calendar.MILLISECOND, -1);
			msg.put("endCreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()));
		}
		if (!StringUtils.isBlank(ifBind)) {
			msg.put("ifBind", ifBind.trim());
		}
		if (!StringUtils.isBlank(dataSource)) {
			List<Erpkind> resultErpKind=erpkindService.selectList(null);
			for(int i=0;i<resultErpKind.size();i++){
	    		if(dataSource.equals(resultErpKind.get(i).getAccountId())){
	    			msg.put("dataSource", resultErpKind.get(i).getDataSource());
	    			}
	    		}
		}
		if (!StringUtils.isBlank(searchKey) && !StringUtils.isBlank(searchWord)) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		return msg;
    }
    
}
